﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnimaGestionPersonnage.Utilities;

namespace AnimaGestionPersonnage.Vue
{
    public partial class selectAdvDisadvForm : Form
    {
        private int etat;

        public selectAdvDisadvForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// un showDialog() personnalise pour nous permettre de choisir les donnees
        /// et la facon d'afficher la fenetre.
        /// </summary>
        /// <param name="advDisadv">flag qui dit si c'est une fenetre d'avantage ou desavantage</param>
        /// <param name="parent">la fenetre parent</param>
        public void ShowDialog(int advDisadv, Form parent)
        {
            etat = advDisadv;

            switch (etat)
            {
                case Utils.ADVANTAGE:
                    this.Text = "Choisir Avantage";

                    foreach (AdvDisadv a in Utils.advDisadvList)
                    {
                        if (a.cost > 0)
                        {
                            listBoxAdvDisadv.Items.Add(a.name + " (" + a.cost + ")");
                        }
                    }

                    base.ShowDialog(parent);
                    break;
                case Utils.DISADVANTAGE:
                    this.Text = "Choisir Désavantage";

                    foreach (AdvDisadv a in Utils.advDisadvList)
                    {
                        if (a.cost < 0)
                        {
                            listBoxAdvDisadv.Items.Add(a.name + " (" + a.cost + ")");
                        }
                    }

                    base.ShowDialog(parent);
                    break;
                default:
                    MessageBox.Show(this, "État non supporté.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    parent.Show();
                    this.Dispose();
                    break;
            }
        }
        
        #region EVENTS BUTTONS
        
        /// <summary>
        /// Verifie l'etat de la fenetre et ajoute l'avantage/desavantage a la bonne liste.
        /// </summary>
        private void buttonSelect_Click(object sender, EventArgs e)
        {
            Boolean doClose = false;
            string[] tempName = ((String)listBoxAdvDisadv.SelectedItem).Split(new Char[] { '(' });

            foreach (AdvDisadv a in Utils.advDisadvList)
            {
                if (a.Equals(new AdvDisadv(tempName[0].Trim())))
                {
                    switch (etat)
                    {
                        case Utils.ADVANTAGE:
                            if (!Utils.charac.advantage.Contains(a))
                            {
                                ((FormPrincStats)this.Owner).addAdvantage(a);
                                doClose = true;
                            }
                            else
                            {
                                MessageBox.Show(this, "Vous avez déjà choisi cet avantage.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            break;
                        case Utils.DISADVANTAGE:
                            if (!Utils.charac.disadvantage.Contains(a))
                            {
                                ((FormPrincStats)this.Owner).addDisadvantage(a);
                                doClose = true;
                            }
                            else
                            {
                                MessageBox.Show(this, "Vous avez déjà choisi cet désavantage.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            break;
                        default:
                            MessageBox.Show(this, "État non supporté.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            break;
                    }
                    break;
                }
            }

            if (doClose)
            {
                this.Owner.Show();
                this.Dispose();
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Owner.Show();
            this.Dispose();
        }
        #endregion
    }
}
